<template>
<view class="container">
  <view class="order-info">
    <view class="item-a">下单时间：{{orderInfo.addTime}}</view>
    <view class="item-b">订单编号：{{orderInfo.orderSn}}</view>
    <view class="item-c">
      <view class="l">实付：
        <text class="cost">￥{{orderInfo.actualPrice}}</text>
      </view>
      <view class="r">
        <!-- <view class="btn active" bindtap="shareGroupon">邀请参团</view> -->
        <view class="btn express" :data-index="orderInfo.id" @tap="detailExpress" v-if="orderInfo.handleOption.confirm">查看物流</view>
        <button class="btn active" open-type="share"> 邀请参团</button>
      </view>
    </view>
  </view>

  <view class="menu-list-pro">
    <view class="h">
      <view class="label">参与团购 ( {{joiners.length}}人)</view>
      <view class="status">查看全部</view>
    </view>
    <view v-for="(item, index) in joiners" :key="index" class="menu-list-item" :data-id="item.id">
      <image class="icon" :src="item.avatar"></image>
      <text class="txt">{{item.nickname}}</text>
    </view>
  </view>

  <view class="order-goods">
    <view class="h">
      <view class="label">商品信息</view>
      <view class="status">{{orderInfo.orderStatusText}}</view>
    </view>
    <view class="goods">
      <view v-for="(item, index) in orderGoods" :key="index" class="item">
        <view class="img">
          <image :src="item.picUrl"></image>
        </view>
        <view class="info">
          <view class="t">
            <text class="name">{{item.goodsName}}</text>
            <text class="number">x{{item.number}}</text>
          </view>
          <view class="attr">{{item.goodsSpecificationValues}}</view>
          <view class="price">￥{{item.retailPrice}}</view>
        </view>
      </view>
    </view>

    <view class="order-bottom">
      <view class="address">
        <view class="t">
          <text class="name">{{orderInfo.consignee}}</text>
          <text class="mobile">{{orderInfo.mobile}}</text>
        </view>
        <view class="b">{{orderInfo.address}}</view>
      </view>
      <view class="total">
        <view class="t">
          <text class="label">商品合计：</text>
          <text class="txt">￥{{orderInfo.goodsPrice}}</text>
        </view>
        <view class="t">
          <text class="label">运费：</text>
          <text class="txt">￥{{orderInfo.freightPrice}}</text>
        </view>
      </view>
      <view class="pay-fee">
        <text class="label">实付：</text>
        <text class="txt">￥{{orderInfo.actualPrice}}</text>
      </view>
    </view>
  </view>

  <!-- </view> -->
</view>
</template>

<script>
let util = require("../../../utils/util.js");
let api = require("../../../config/api.js");

export default {
  data() {
    return {
      id: 0,
      orderId: 0,
      groupon: {},
      linkGrouponId: 0,
      joiners: [],
      orderInfo: {},
      orderGoods: [],
      expressInfo: {},
      flag: false,
      handleOption: {}
    };
  },

  components: {},
  props: {},
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
    this.setData({
      id: options.id
    });
    this.getOrderDetail();
  },
  // 页面分享
  onShareAppMessage: function () {
    let that = this;
    return {
      title: '邀请团购',
      desc: '聚惠星团购分享',
      path: '/pages/index/index?grouponId=' + this.linkGrouponId
    };
  },

  onPullDownRefresh() {
    uni.showNavigationBarLoading(); //在标题栏中显示加载

    this.getOrderDetail();
    uni.hideNavigationBarLoading(); //完成停止加载

    uni.stopPullDownRefresh(); //停止下拉刷新
  },

  onReady: function () {// 页面渲染完成
  },
  onShow: function () {// 页面显示
  },
  onHide: function () {// 页面隐藏
  },
  onUnload: function () {// 页面关闭
  },
  methods: {
    shareGroupon: function () {
      let that = this;
      uni.showActionSheet({
        itemList: ['分享给朋友', '分享到朋友圈'],
        success: function (res) {
          if (res.tapIndex == 0) {
            uni.showModal({
              title: '提示',
              content: '点击右上角 "..." 转发给朋友',
              showCancel: false
            });
          } else if (res.tapIndex == 1) {
            that.saveShare();
          } else {
            console.log(res.tapIndex);
          }
        },
        fail: function (res) {
          console.log(res.errMsg);
        }
      });
    },
    // 保存分享图
    saveShare: function () {
      let that = this;
      uni.downloadFile({
        url: that.groupon.shareUrl,
        success: function (res) {
          console.log(res);
          uni.saveImageToPhotosAlbum({
            filePath: res.tempFilePath,
            success: function (res) {
              uni.showModal({
                title: '存图成功',
                content: '图片成功保存到相册了，可以分享到朋友圈了',
                showCancel: false,
                confirmText: '好的',
                confirmColor: '#a78845',
                success: function (res) {
                  if (res.confirm) {
                    console.log('用户点击确定');
                  }
                }
              });
            },
            fail: function (res) {
              console.log('fail');
            }
          });
        },
        fail: function () {
          console.log('fail');
        }
      });
    },
    detailExpress: function (e) {
      let orderId = e.currentTarget.dataset.index;
      uni.navigateTo({
        url: '/pages/ucenter/expressInfo/expressInfo?orderId=' + orderId
      });
    },
    //获取物流信息
    getOrderExpress: function () {
      let that = this;
      util.request(api.ExpressQuery, {
        expCode: that.orderInfo.expCode,
        expNo: that.orderInfo.expNo
      }, 'POST').then(function (res) {
        if (res.errno === 0) {
          that.setData({
            expressInfo: res.data
          });
        }
      });
    },
    expandDetail: function () {
      let that = this;
      this.setData({
        flag: !that.flag
      });
    },
    getOrderDetail: function () {
      let that = this;
      util.request(api.GroupOnDetail, {
        grouponId: that.id
      }).then(function (res) {
        if (res.errno === 0) {
          that.setData({
            joiners: res.data.joiners,
            groupon: res.data.groupon,
            linkGrouponId: res.data.linkGrouponId,
            orderId: res.data.orderInfo.id,
            orderInfo: res.data.orderInfo,
            orderGoods: res.data.orderGoods,
            handleOption: res.data.orderInfo.handleOption
          });
        }
      });
    },
    // “去付款”按钮点击效果
    payOrder: function () {
      let that = this;
      let topic = uni.getStorageSync('topic')
            if (!topic) {
              console.log('topic id err')
              return
            }

      util.request(api.OrderPrepay, {
        topicId: topic.id,
        orderId: that.orderId
        
      }, 'POST').then(function (res) {
        if (res.errno === 0) {
          const payParam = res.data;
          console.log("支付过程开始");
          uni.requestPayment({
            'timeStamp': payParam.timeStamp,
            'nonceStr': payParam.nonceStr,
            'package': payParam.packageValue,
            'signType': payParam.signType,
            'paySign': payParam.paySign,
            'success': function (res) {
              console.log("支付过程成功");
              util.redirect('/pages/ucenter/order/order');
            },
            'fail': function (res) {
              console.log("支付过程失败");
              util.showErrorToast('支付失败');
            },
            'complete': function (res) {
              console.log("支付过程结束");
            }
          });
        }
      });
    },
    // “取消订单”点击效果
    cancelOrder: function () {
      let that = this;
      let orderInfo = that.orderInfo;
      uni.showModal({
        title: '',
        content: '确定要取消此订单？',
        success: function (res) {
          if (res.confirm) {
            util.request(api.OrderCancel, {
              orderId: orderInfo.id
            }, 'POST').then(function (res) {
              if (res.errno === 0) {
                uni.showToast({
                  title: '取消订单成功'
                });
                util.redirect('/pages/ucenter/order/order');
              } else {
                util.showErrorToast(res.errmsg);
              }
            });
          }
        }
      });
    },
    // “取消订单并退款”点击效果
    refundOrder: function () {
      let that = this;
      let orderInfo = that.orderInfo;
      uni.showModal({
        title: '',
        content: '确定要取消此订单？',
        success: function (res) {
          if (res.confirm) {
            util.request(api.OrderRefund, {
              orderId: orderInfo.id
            }, 'POST').then(function (res) {
              if (res.errno === 0) {
                uni.showToast({
                  title: '取消订单成功'
                });
                util.redirect('/pages/ucenter/order/order');
              } else {
                util.showErrorToast(res.errmsg);
              }
            });
          }
        }
      });
    },
    // “删除”点击效果
    deleteOrder: function () {
      let that = this;
      let orderInfo = that.orderInfo;
      uni.showModal({
        title: '',
        content: '确定要删除此订单？',
        success: function (res) {
          if (res.confirm) {
            util.request(api.OrderDelete, {
              orderId: orderInfo.id
            }, 'POST').then(function (res) {
              if (res.errno === 0) {
                uni.showToast({
                  title: '删除订单成功'
                });
                util.redirect('/pages/ucenter/order/order');
              } else {
                util.showErrorToast(res.errmsg);
              }
            });
          }
        }
      });
    },
    // “确认收货”点击效果
    confirmOrder: function () {
      let that = this;
      let orderInfo = that.orderInfo;
      uni.showModal({
        title: '',
        content: '确认收货？',
        success: function (res) {
          if (res.confirm) {
            util.request(api.OrderConfirm, {
              orderId: orderInfo.id
            }, 'POST').then(function (res) {
              if (res.errno === 0) {
                uni.showToast({
                  title: '确认收货成功！'
                });
                util.redirect('/pages/ucenter/order/order');
              } else {
                util.showErrorToast(res.errmsg);
              }
            });
          }
        }
      });
    }
  }
};
</script>
<style>
page {
  height: 100%;
  width: 100%;
  background: #f4f4f4;
}

.order-info {
  padding-top: 25rpx;
  background: #fff;
  height: auto;
  overflow: hidden;
}

.item-a {
  padding-left: 31.25rpx;
  height: 42.5rpx;
  padding-bottom: 12.5rpx;
  line-height: 30rpx;
  font-size: 30rpx;
  color: #666;
}

.item-b {
  padding-left: 31.25rpx;
  height: 29rpx;
  line-height: 29rpx;
  margin-top: 12.5rpx;
  margin-bottom: 41.5rpx;
  font-size: 30rpx;
  color: #666;
}

.item-c {
  margin-left: 31.25rpx;
  border-top: 1px solid #f4f4f4;
  height: 103rpx;
  line-height: 103rpx;
}

.item-c .l {
  float: left;
}

.item-c .r {
  height: 103rpx;
  float: right;
  display: flex;
  align-items: center;
  padding-right: 16rpx;
}

.item-c .r .btn {
  float: right;
}

.item-c .cost {
  color: #b4282d;
}

.item-c .btn {
  line-height: 66rpx;
  border-radius: 5rpx;
  text-align: center;
  margin: 0 15rpx;
  padding: 0 20rpx;
  height: 66rpx;
}

.item-c .btn.active {
  background: #ee9e09;
  color: #fff;
  font-size: 30rpx;
}

.item-c .btn.express {
  background: #FF654B;
  color: #fff;
  font-size: 30rpx;
}

.order-goods {
  margin-top: 20rpx;
  background: #fff;
}

.order-goods .h {
  height: 93.75rpx;
  line-height: 93.75rpx;
  margin-left: 31.25rpx;
  border-bottom: 1px solid #f4f4f4;
  padding-right: 31.25rpx;
}

.order-goods .h .label {
  float: left;
  font-size: 30rpx;
  color: #333;
}

.order-goods .h .status {
  float: right;
  font-size: 30rpx;
  color: #b4282d;
}

.order-goods .item {
  display: flex;
  align-items: center;
  height: 192rpx;
  margin-left: 31.25rpx;
  padding-right: 31.25rpx;
  border-bottom: 1px solid #f4f4f4;
}

.order-goods .item:last-child {
  border-bottom: none;
}

.order-goods .item .img {
  height: 145.83rpx;
  width: 145.83rpx;
  background: #f4f4f4;
}

.order-goods .item .img image {
  height: 145.83rpx;
  width: 145.83rpx;
}

.order-goods .item .info {
  flex: 1;
  height: 145.83rpx;
  margin-left: 20rpx;
}

.order-goods .item .t {
  margin-top: 8rpx;
  height: 63rpx;
  line-height: 33rpx;
  margin-bottom: 10.5rpx;
}

.order-goods .item .t .name {
  display: block;
  float: left;
  height: 63rpx;
  line-height: 33rpx;
  color: #333;
  font-size: 30rpx;
}

.order-goods .item .t .number {
  display: block;
  float: right;
  height: 63rpx;
  text-align: right;
  line-height: 33rpx;
  color: #333;
  font-size: 30rpx;
}

.order-goods .item .attr {
  height: 29rpx;
  line-height: 29rpx;
  color: #666;
  margin-bottom: 25rpx;
  font-size: 25rpx;
}

.order-goods .item .price {
  display: block;
  float: left;
  height: 30rpx;
  line-height: 30rpx;
  color: #333;
  font-size: 30rpx;
}

.order-goods .item .btn {
  height: 50rpx;
  line-height: 50rpx;
  border-radius: 5rpx;
  text-align: center;
  display: block;
  float: right;
  margin: 0 15rpx;
  padding: 0 20rpx;
}

.order-goods .item .btn.active {
  background: #b4282d;
  color: #fff;
}

.order-bottom {
  margin-top: 20rpx;
  padding-left: 31.25rpx;
  height: auto;
  overflow: hidden;
  background: #fff;
}

.order-bottom .address {
  height: 128rpx;
  padding-top: 25rpx;
  border-bottom: 1px solid #f4f4f4;
}

.order-bottom .address .t {
  height: 35rpx;
  line-height: 35rpx;
  margin-bottom: 7.5rpx;
}

.order-bottom .address .name {
  display: inline-block;
  height: 35rpx;
  width: 140rpx;
  line-height: 35rpx;
  font-size: 30rpx;
}

.order-bottom .address .mobile {
  display: inline-block;
  height: 35rpx;
  line-height: 35rpx;
  font-size: 30rpx;
}

.order-bottom .address .b {
  height: 35rpx;
  line-height: 35rpx;
  font-size: 30rpx;
}

.order-bottom .total {
  height: 106rpx;
  padding-top: 20rpx;
  border-bottom: 1px solid #f4f4f4;
}

.order-bottom .total .t {
  height: 30rpx;
  line-height: 30rpx;
  margin-bottom: 7.5rpx;
  display: flex;
}

.order-bottom .total .label {
  width: 150rpx;
  display: inline-block;
  height: 35rpx;
  line-height: 35rpx;
  font-size: 30rpx;
}

.order-bottom .total .txt {
  flex: 1;
  display: inline-block;
  height: 35rpx;
  line-height: 35rpx;
  font-size: 30rpx;
}

.order-bottom .pay-fee {
  height: 81rpx;
  line-height: 81rpx;
}

.order-bottom .pay-fee .label {
  display: inline-block;
  width: 140rpx;
  color: #b4282d;
}

.order-bottom .pay-fee .txt {
  display: inline-block;
  width: 140rpx;
  color: #b4282d;
}

.order-express {
  margin-top: 20rpx;
  width: 100%;
  height: 100rpx;
  background: #fff;
}

.order-express .expand {
  /* margin-top: 20rpx; */
  width: 100%;
  height: 100rpx;
  background: #fff;
  /* border: 10rpx #a78845; */
}

.order-express .title {
  float: left;
  margin-bottom: 20rpx;
  padding: 10rpx;
}

.order-express .ti {
  float: right;
  width: 52rpx;
  height: 52rpx;
  margin-right: 16rpx;
  margin-top: 28rpx;
}

.order-express .t {
  font-size: 29rpx;
  margin-left: 10.25rpx;
  color: #a78845;
}

.order-express .b {
  font-size: 29rpx;
  margin-left: 10.25rpx;
  color: #a78845;
}

.order-express .traces {
  padding: 17.5rpx;
  background: #fff;
  border-bottom: 1rpx solid #f1e6cdcc;
}

.order-express .trace {
  padding-bottom: 17.5rpx;
  padding-top: 17.5rpx;
  background: #fff;
}

.order-express .acceptTime {
  margin-top: 20rpx;
  margin-right: 40rpx;
  text-align: right;
  font-size: 26rpx;
}

.order-express .acceptStation {
  font-size: 26rpx;
}

.menu-list-pro {
  margin-top: 20rpx;
  overflow-x: scroll;
  white-space: nowrap;
  text-overflow: ellipsis;
  height: 260rpx;
  width: 100%;
  overflow: hidden;
  border-bottom: 1rpx #cfc9ca;
  background-color: #fff;
}

.menu-list-pro .h {
  height: 93.75rpx;
  line-height: 93.75rpx;
  margin-left: 31.25rpx;
  border-bottom: 1px solid #f4f4f4;
  padding-right: 31.25rpx;
}

.menu-list-pro .h .label {
  float: left;
  font-size: 30rpx;
  color: #333;
}

.menu-list-pro .h .status {
  float: right;
  font-size: 30rpx;
  color: #a78845;
}

.menu-list-pro  .menu-list-item {
  display: block;
  float: left;
  height: 110rpx;
  width: 80rpx;
  margin-top: 30rpx;
  margin-bottom: 30rpx;
  margin-left: 40rpx;
}

.menu-list-pro .icon {
  height: 80rpx;
  width: 80rpx;
  border-radius: 12rpx;
  box-shadow: 0px 4rpx 4rpx 0px #cfc9ca;
}

.menu-list-pro .txt {
  display: block;
  float: left;
  width: 80rpx;
  margin-top: 5rpx;
  font-size: 22rpx;
  color: #a78845;
}

</style>